import { defineStore } from 'pinia'
import { ipc, isEE } from "@/utils/ipcRenderer";
import { ipcApiRoute } from "@/api";
import { setToken } from '@/utils/auth'
import { ref } from 'vue'
export const useUserStore = defineStore('user', () => {

    const USER_KEY = "fdauto-userinfo"

    //登录
    async function login(userName, password) {
        const result = await ipc.invoke(ipcApiRoute.login, {
            userName, password
        })
        if (result.success) {
            setToken(result.cookie)
        }

        return result;
    }


    async function getUserInfo() {
        if (!isEE) {
            return {
                id: "01521",
                loginTime:new Date("2025-07-14 15:10:03"),
                cookie: "33333333"
            }
        }

        const userInfo = await ipc.invoke(ipcApiRoute.getUserInfo)
        return userInfo


    }

    async function logout(params) {
        if (isEE) {
            await ipc.invoke(ipcApiRoute.logout)
            setToken("")
        }
    }


    return { getUserInfo, login, logout }
})